package UserInterface;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.text.MaskFormatter;

public class PutPinCode extends JPanel {
	private JFormattedTextField passwordField;
	String s="";
	MaskFormatter formatter;
	private NumberPanel panelNumbers =  new NumberPanel();
	private EditorsPanel panelEditors = new EditorsPanel();
	public void putNumbers(){
		JButton[] buttons =panelNumbers.getButtons();
		for(int i=0;i<buttons.length;i++){
			buttons[i].addActionListener(new Listen(buttons[i].getText()));
		}
	}
	class Listen implements ActionListener{
		String text="";
		Listen(String text){
			this.text=text;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (s.length()<4)
			{
			s+=text;
			passwordField.setText(s);
			}
			
		}

		
		
	}
	
	public void clearS(){
		s="";
	}
	public void setS(String s){
		this.s=s;
		passwordField.setText(s);
	}
	public String getS(){
		return s;
	}
	/**
	 * Create the panel.
	 */
	public void putNumbersListener(ActionListener list){
		panelNumbers.addActionListener(list);
	}
	public void putAcountNumber(String number){
		passwordField.setText(number);
	}
	public String getAcountNumber(){
		return passwordField.getText();
	}
	
	public void putListenerOk(ActionListener list){
		panelEditors.setOkListener(list);
	}
	public void putListenerCancel(ActionListener list){
		panelEditors.setCancelListener(list);
	}
	public void putListenerChange(ActionListener list){
		panelEditors.setChangeListener(list);
	}
	
		
	public PutPinCode() {
	
	 
     this.setSize(500, 500);
     
     setBackground(new Color(255, 248, 220));
     JLabel lblEnterPin = new JLabel("Enter PIN Code:");
     lblEnterPin.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 29));
     lblEnterPin.setForeground(new Color(128, 0, 0));
     
    
     panelEditors.setBackground(new Color(255, 248, 220));
     
     try {
			formatter = new MaskFormatter("####");
			//formatter.setPlaceholderCharacter('_');
		   
			passwordField = new JFormattedTextField(formatter);
			passwordField.setFont(new Font("Tahoma", Font.BOLD, 19));
			passwordField.setColumns(10);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     
     GroupLayout groupLayout = new GroupLayout(this);
     groupLayout.setHorizontalGroup(
     	groupLayout.createParallelGroup(Alignment.TRAILING)
     		.addGroup(groupLayout.createSequentialGroup()
     			.addGap(27)
     			.addComponent(panelNumbers, GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
     			.addGap(18)
     			.addComponent(panelEditors, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
     			.addContainerGap())
     		.addGroup(groupLayout.createSequentialGroup()
     			.addGap(129)
     			.addComponent(passwordField)
     			.addGap(121))
     		.addGroup(groupLayout.createSequentialGroup()
     			.addContainerGap(115, Short.MAX_VALUE)
     			.addComponent(lblEnterPin)
     			.addGap(136))
     );
     groupLayout.setVerticalGroup(
     	groupLayout.createParallelGroup(Alignment.LEADING)
     		.addGroup(groupLayout.createSequentialGroup()
     			.addGap(30)
     			.addComponent(lblEnterPin)
     			.addGap(42)
     			.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
     			.addGap(38)
     			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
     				.addComponent(panelEditors, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
     				.addComponent(panelNumbers, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE))
     			.addContainerGap(115, Short.MAX_VALUE))
     );
     setLayout(groupLayout);
     
     
	}
	
}
